package 力扣.比赛;

import sun.awt.image.IntegerComponentRaster;

import java.util.Arrays;
import java.util.PriorityQueue;

public class 雪糕的最大数量1833 {
    //top k问题 ——最小值
    public int maxIceCream(int[] costs, int coins) {
       if (costs == null || costs.length == 0){
           return 0;
       }
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(
                (Integer o1, Integer o2) ->{
                    return o1 - o2;//小根堆
                }
        );

        for (int i = 0; i < costs.length; i++) {
            int c = costs[i];
            priorityQueue.add(c);
        }
        int p = 0;
        int ans = 0;
        while (!priorityQueue.isEmpty() && p < coins){
            Integer c = priorityQueue.poll();
            p += c;
            if (p <= coins){
                ans++;
            }else {
                return ans;
            }
        }
        return ans;
    }

    public int maxIceCream2(int[] costs, int coins) {
        if (costs == null || costs.length == 0){
            return 0;
        }
        Arrays.sort(costs);
        int ans = 0;
        int i = 0;
        while ( i < costs.length && coins >= costs[i] ){
            coins -= costs[i++];
            ans++;
        }
        return ans;
    }
}
